package com.example.volunteeroa.controller;

import com.example.volunteeroa.JpaUtils.DeleteUtils;
import com.example.volunteeroa.entity.*;
import com.example.volunteeroa.repository.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@CrossOrigin//跨域
@RestController
@RequestMapping("api/venue")
public class VenueController {
    @Resource
    VenueRepository VERepository;
    @Resource
    OrganizationRepository ORepository;
    @Resource
    VolunteerRepository VRepository;
    @Resource
    VenueAllocationRepository VARepository;
    @Resource
    DivisionRepository DRepository;
    @PostMapping("/findVenue")
    public RestBean findVenueByID(@RequestBody Venue venue) {
        Optional<Venue> byID = VERepository.findById(venue.getVenueID());
        if (byID.isPresent()) {
            Venue venue1 = byID.get();
            return new RestBean(200, "通过场馆(venue)ID查询场馆成功", venue1);
        } else {
            return new RestBean(500, "所查询的场馆ID不存在", null);
        }
    }//通过场馆ID查询场馆

    @GetMapping()
    public RestBean findAll() {
        List<Venue> all = VERepository.findAll();
        return new RestBean(200, "查询全部场馆(venue)成功", all);
    }//查询全部场馆(venue)

    @DeleteMapping()
    public RestBean deleteVenueByID(@RequestBody Venue venue) {
        DeleteUtils.deleteVenueAllocByVenueID(VERepository,VARepository,venue);
        DeleteUtils.deleteVenue(VERepository, ORepository, VRepository, venue);
        return new RestBean(200, "删除场馆成功", null);
    }//通过场馆(Venue)名字删除场馆信息

    @PostMapping("/{did}")
    public RestBean add(@RequestBody Venue venue,@PathVariable Integer did) {
        Division division = DRepository.findById(did).get();
        venue.setDivision(division);
        VERepository.save(venue);
        return new RestBean(200, "添加成功", null);
    }//添加一个场馆的信息

    //由于场馆下的组织数量由organization表中对应的场馆ID决定
    //暂无修改场馆下的组织数量这一功能
}

